package sender

import (
	common "Monitor/common"
	agent "Monitor/go-zabbix-proto/agent"

	"time"

	"github.com/gogf/gf/os/gcfg"
)

type ZabbixSender struct {
	Server string
	Port   int
	Name   string
	Agent  *agent.Agent
	Data   []*agent.Metric
	Logger *common.CommonLogger
}

func NewAgent() (z *ZabbixSender) {
	z = &ZabbixSender{
		Logger: common.BLogger,
	}
	return
}
func (z *ZabbixSender) SetAgent() {
	z.Name = gcfg.Instance().GetString("zabbix_agent_host")
	z.Server = gcfg.Instance().GetString("zabbix_server")
	z.Port = gcfg.Instance().GetInt("zabbix_port")
	z.Agent = agent.NewAgent(z.Name, z.Server, z.Port)
}

func (z *ZabbixSender) AddData(key string, value string) *agent.Metric {
	return z.Agent.NewMetric(key, value, time.Now().Unix())
}

func (z *ZabbixSender) Sender(data []*agent.Metric) {
	var err error
	// Sending metrics to Zabbix
	// var data []*agent.Metric
	// data = append(data, agent.NewMetric("agent.ping", "10", time.Now().Unix()))
	// data = append(data, agent.NewMetric("agent.hostname", agentName))
	// data := z.AddData("ping_max", "100")
	var res *agent.Response
	res, err = z.Agent.Send(data)
	if err != nil {
		z.Logger.Error(err.Error())
	} else {
		z.Logger.Info("Received response: %s", res.JSON)
	}
}
